
$(document).ready(function(){
    doShopCarReady();
});

//==================================购物车相关=======================================


//初始化购物车显示信息
function doShopCarReady() {
    $.ajax({
        type: "post",
        url: '/shop/getShopCarDetailInfo.do',
        dataType: "json",
        data: {
        },
        success: function(resp){
            console.log(resp);
            if(resp.result==="success"){
                showChopCarInfo(resp.shop_car_info);
            }
        }
    });
}

//添加商品到购物车
function addToShopCar(goods_no) {
    $.ajax({
        type: "post",
        url: '/shop/addToShopCarDetail.do',
        dataType: "json",
        data: {
            'goods_no':goods_no,
            'goods_num':1,
        },
        success: function(resp){
            console.log(resp);
            if(resp.result==="success"){
                showChopCarInfo(resp.shop_car_info,goods_no);
            }else{
                alertTips("添加到购物车异常！")
            }
        }
    });
}

//从购物车删除商品
function subFromShopCar(goods_no) {
    $.ajax({
        type: "post",
        url: '/shop/subFromShopCarDetail.do',
        dataType: "json",
        data: {
            'goods_no':goods_no,
            'goods_num':1
        },
        success: function(resp){
            console.log(resp);
            if(resp.result==="success"){
                showChopCarInfo(resp.shop_car_info,goods_no);
            }else{
            }
        }
    });
}


//显示购物车信息
function showChopCarInfo(shop_car_info,goods_no) {
    var html = "";
    if(shop_car_info.goods_list.length>0){
        for (var i=0;i<shop_car_info.goods_list.length;i++){
            var sale_html ="";
            html = html+"<li>\n" +
                "<div class=\"pro_checkbox_box\"><input onclick='checkIsAllSelect()' price='"+shop_car_info.goods_list[i].goods_price+"' " +
                "num = '"+shop_car_info.goods_list[i].goods_num+"' type=\"checkbox\" name='selected_goods' checked='checked' value='"+shop_car_info.goods_list[i].goods_no+"' /></div>\n" +
                "<div class=\"pro_img_box\"><img src="+shop_car_info.goods_list[i].goods_img+" width=\"70\" height=\"70\" /></div>\n" +
                "<div class=\"pro_info_box\">\n" +
                "<strong>"+shop_car_info.goods_list[i].goods_name+"</strong>\n" +
                "<span>¥"+shop_car_info.goods_list[i].goods_price+"</span>\n" +
                "</div>\n" +
                "<div class=\"pro_info_box bottom\">\n" +
                "<em>规格："+shop_car_info.goods_list[i].goods_specifications+"</em>\n" +
                "<div class=\"increase_box\">\n" +
                "<div class=\"after_plus\" style=\"opacity: 1; right: 30px;\">\n" +
                "<i class=\"iconfont icon-jian jian\" onclick=\"subFromShopCar('"+shop_car_info.goods_list[i].goods_no+"')\"></i>\n" +
                "<span class=\"num\">"+shop_car_info.goods_list[i].goods_num+"</span>\n" +
                "</div>\n" +
                "<i class=\"iconfont icon-jia plus\" onclick=\"addToShopCar('"+shop_car_info.goods_list[i].goods_no+"')\"></i>\n" +
                "</div>\n" +
                "</div>\n";
            if(checkIsNotNull(shop_car_info.goods_list[i].sale_activities) && shop_car_info.goods_list[i].sale_activities.length>0){
                sale_html = "<div class=\"free_info_box\">" +
                            "<em>促</em>" +
                            "<span>该商品共有"+shop_car_info.goods_list[i].sale_activities.length+"个促销活动</span>" +
                            "<i class=\"iconfont icon-xiala\"></i>" +
                            "</div>" +
                            "<div class=\"free_info_detil\" style=\"display: block;\">";
                for(var j=0;j<shop_car_info.goods_list[i].sale_activities.length;j++){
                    if(shop_car_info.goods_list[i].sale_activities[j].sale_type==='PRICE_OFF'){
                        sale_html = sale_html+"<p>满"+shop_car_info.goods_list[i].sale_activities[j].buy_num+"箱减"+shop_car_info.goods_list[i].sale_activities[j].off_price+"元</p>";
                    }else{
                        sale_html = sale_html+"<p>满"+shop_car_info.goods_list[i].sale_activities[j].buy_num+"箱赠"+shop_car_info.goods_list[i].sale_activities[j].giveaway_num+"件" +
                            shop_car_info.goods_list[i].sale_activities[j].giveaway_name+"</p>";
                    }
                }
                sale_html = sale_html +"</div>";
            }

            html = html+ sale_html+ "</li>";
        }
    }else{
        location.href=ctx+"/shop/shopIndex.do";
    }
    $('#shop_car_list').html(html);
    showTotalAmount();
}

//获取已经选择的商品
function getSelectedGoods() {
    var goods_no_array = new Array();
    $('input[name="selected_goods"]:checked').each(function(){
        goods_no_array.push($(this).val());
    });
    if(goods_no_array.length<1){
        alertTips("请选择结算的商品！");
    }else{
        return goods_no_array;
    }
}

//获取合计价格
function showTotalAmount() {
    var price = 0;
    $('input[name="selected_goods"]:checked').each(function(){
        price = price+parseFloat($(this).attr('price'))*parseFloat($(this).attr('num'));
    });
    $('#car_price').html(price.toFixed(2));
}


//检查是否全选
function checkIsAllSelect() {
    var i = 0;
    $('input[name="selected_goods"]:checked').each(function(){
        i++;
    });
    var a = 0;
    $('input[name="selected_goods"]').each(function(){
        a++;
    });
    if(a!==i){
        $('#all_chose').find('input').attr('checked',false);
    }else{
        $('#all_chose').find('input').attr('checked',true);
    }
    showTotalAmount();
}

//下单
function toCreateOrder() {
    var goods_no_array = getSelectedGoods();
    location.href=ctx+'/shop/toCreateOrder.do?goods_no_list='+JSON.stringify(goods_no_array);
}



//==================================================================================